package com.heisen.file.controller;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/test")
public class TestController {

    private static final String accessKey = "4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT";
    private static final String secretKey = "WEGG_z1YXQ9Ck4hHZSS-qOedeVCtJNw2ngcKxQWX";
    private static final String bucket = "aike-info";

    private static final String fileKey = "test0034001.png";

    @GetMapping
    public String testGet() throws UnsupportedEncodingException {

		// 获取文件路径时需要写这个桶域名，然后做替换，这个桶域名会被用于加密。所以需要后替换
        String domainOfBucket = "http://aike-info-qos.iot-st4.cmiot-solutions.com";
        String encodedFileName = URLEncoder.encode(fileKey, "utf-8").replace("+", "%20");
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);

        Auth auth = Auth.create(accessKey, secretKey);
        //1小时，可以自定义链接过期时间
        long expireInSeconds = 36000;
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);
        String nginxUrl = finalUrl.replaceFirst("http://aike-info-qos.iot-st4.cmiot-solutions.com",
                "http://218.201.45.176:8008/aikeInfoQos");
        System.out.println(nginxUrl);
        return "GET";
    }


    /**
    * 获取加密图片地址
    * @throws QiniuException
    * @throws UnsupportedEncodingException
    */
   private static void downloadSec() throws QiniuException, UnsupportedEncodingException {


      //https://qn-oss.onepark.com.cn:8008/aikeInfoQos/zhy-oss/ape-parking/pms-test/2024/02/29/loGP10Xr0r3docnPNHqVBrSMkXI=.jpg?e=2024536308&token=4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT:n6oaucPVPMjhchthrGPY6zOhDU8=
      // http://aike-info-qos.iot-st4.cmiot-solutions.com/XXX.jpg
      String fileName = "zhy-oss/ape-parking/pms-test/2024/02/29/test-test-testloGP10Xr0r3docnPNHqVBrSMkXI=.jpg";
//      String fileName = "XXX.jpg";
      String domainOfBucket = "http://aike-info-qos.iot-st4.cmiot-solutions.com";
//      String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
      String publicUrl = String.format("%s/%s", domainOfBucket, fileName);

      String accessKey = "4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT";
      String secretKey = "WEGG_z1YXQ9Ck4hHZSS-qOedeVCtJNw2ngcKxQWX";
      Auth auth = Auth.create(accessKey, secretKey);
      //1小时，可以自定义链接过期时间
      long expireInSeconds = 36000;
      String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
      System.out.println(finalUrl);
      String nginxUrl = finalUrl.replaceFirst("http://aike-info-qos.iot-st4.cmiot-solutions.com",
              "https://qn-oss.onepark.com.cn:8008/aikeInfoQos");
      System.out.println(nginxUrl);

   }

    @PostMapping
    public String testPost(@RequestParam("file") MultipartFile file) throws IOException {

        Configuration cfg = new Configuration(new Region.Builder()
                .srcUpHost("http://218.201.45.176:8008/upQos/")
                .accUpHost("http://218.201.45.176:8008/upQos/")
                .build());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
        UploadManager manager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        Response put = manager.put(file.getBytes(), fileKey, upToken);

        System.out.println(put.toString());
        return "POST";
    }


    /**
     * 获取加密图片地址
     * @throws UnsupportedEncodingException
     */
    private static void downloadSec1() throws QiniuException, UnsupportedEncodingException {

      //  String  s ="http://aike-info-qos.iot-st4.cmiot-solutions.com/zhy-oss/ape-parking/48A2B8FF69FB/20240304143852_1699609185505188_%E8%B4%B5N7U0P5_xt.jpg?e=1709537942&token=4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT:oC_vUlNVSCb4AyDd6359N9GpjHA=";

        //https://qn-oss.onepark.com.cn:8008/aikeInfoQos/zhy-oss/ape-parking/pms-test/2024/02/29/loGP10Xr0r3docnPNHqVBrSMkXI=.jpg?e=2024536308&token=4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT:n6oaucPVPMjhchthrGPY6zOhDU8=
        // http://aike-info-qos.iot-st4.cmiot-solutions.com/XXX.jpg
        String fileName = "zhy-oss/ape-parking/48A2B8FF69FB/20240304143852_1699609185505188_%E8%B4%B5N7U0P5_xt.jpg";
//      String fileName = "XXX.jpg";
        String domainOfBucket = "http://aike-info-qos.iot-st4.cmiot-solutions.com";
//      String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
        String publicUrl = String.format("%s/%s", domainOfBucket, fileName);
        System.out.println(publicUrl);
        String accessKey = "4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT";
        String secretKey = "WEGG_z1YXQ9Ck4hHZSS-qOedeVCtJNw2ngcKxQWX";
        Auth auth = Auth.create(accessKey, secretKey);
        //1小时，可以自定义链接过期时间
        long expireInSeconds = 36000;
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);

        String nginxUrl = finalUrl.replaceFirst("http://aike-info-qos.iot-st4.cmiot-solutions.com",
                "https://qn-oss.onepark.com.cn:8008/aikeInfoQos");
        System.out.println(nginxUrl);

    }

    public static void main(String[] args) {
        try {
           downloadSec1();
        } catch (QiniuException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
